﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Referee
{
    static class Similarity
    {
        public static double getSimilarity(string myCode, string hisCode, out double hisim)
        {
            if (!myCode.EndsWith("\r\n"))
                myCode += "\r\n";
            if (!hisCode.EndsWith("\r\n"))
                hisCode += "\r\n";
            //去除空格
            myCode = myCode.Replace(" ", "");
            hisCode = hisCode.Replace(" ", "");
            //去除制表符
            myCode = myCode.Replace("\t", "");
            hisCode = hisCode.Replace("\t", "");
            //去掉{}
            myCode = myCode.Replace("{", "");
            hisCode = hisCode.Replace("{", "");
            myCode = myCode.Replace("}", "");
            hisCode = hisCode.Replace("}", "");
            //去除#include<>
            string tempStr = null;
            for (int i = 0; i < myCode.Length; ++i)
            {
                if (myCode[i] == '#')
                {
                    while (myCode[i] != '\n')
                        ++i;
                    ++i;
                }
                tempStr += myCode[i];
            }
            myCode = tempStr;
            tempStr = null;
            for (int i = 0; i < hisCode.Length; ++i)
            {
                if (hisCode[i] == '#')
                {
                    while (hisCode[i] != '\n')
                        ++i;
                    ++i;
                }
                tempStr += hisCode[i];
            }
            hisCode = tempStr;
            //去除system("pause");
            myCode = myCode.Replace("system(\"pause\");\r\n", "");
            hisCode = hisCode.Replace("system(\"pause\");\r\n", "");
            //去除using namespace std;
            myCode = myCode.Replace("using namespace std;\r\n", "");
            hisCode = hisCode.Replace("using namespace std;\r\n", "");
            //去除空行
            Regex spaceRegex = new Regex(@"\n[\s| ]*\r");
            myCode = spaceRegex.Replace(myCode, "");
            hisCode = spaceRegex.Replace(hisCode, "");
            //获取两个字符串的哈希值的排序数组
            List<int> preHash = new List<int>();
            var psr = new System.IO.StringReader(myCode);
            tempStr = null;
            while (true)
            {
                tempStr = psr.ReadLine();
                if (tempStr == null)
                    break;
                preHash.Add(tempStr.GetHashCode());
            }
            List<int> codeHash = new List<int>();
            var sr = new System.IO.StringReader(hisCode);
            tempStr = null;
            while (true)
            {
                tempStr = sr.ReadLine();
                if (tempStr == null)
                    break;
                codeHash.Add(tempStr.GetHashCode());
            }
            codeHash.Sort();
            preHash.Sort();
            int indexp = 0, indexc = 0, sml = 0;
            while (indexp != preHash.Count() && indexc != codeHash.Count())
            {
                if (preHash[indexp] > codeHash[indexc])
                    indexc++;
                else if (preHash[indexp] < codeHash[indexc])
                    indexp++;
                else
                {
                    sml++;
                    indexp++;
                    indexc++;
                }
            }
            var maxSml = Math.Max((double)sml / codeHash.Count(),(double)sml / preHash.Count());
            hisim = maxSml;
            return maxSml;
        }
    }
}
